//
//  MTRegionViewController.m
//  美团HD
//
//  Created by kongxiaopeng on 15/6/23.
//  Copyright (c) 2015年 njsk. All rights reserved.
//

#import "MTRegionViewController.h"
#import "MTHomeDropdown.h"
#import "MTCityViewController.h"
#import "MTNavigationController.h"
#import "UIView+Extension.h"
#import "MTRegion.h"
#import "MTConst.h"
@interface MTRegionViewController ()<MTHomeDropdownDataSource,MTHomeDropdownDelegate>
- (IBAction)changeCity;

@end

@implementation MTRegionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建下拉菜单
    UIView* title = [self.view.subviews firstObject];
    MTHomeDropdown* dropdown = [MTHomeDropdown dropdown];
    dropdown.y = title.height;
    dropdown.dataSource = self;
    dropdown.delegate = self;
    [self.view addSubview:dropdown];
    
    
    //设置控制器在popover中得尺寸
    self.preferredContentSize = CGSizeMake(dropdown.width, CGRectGetMaxY(dropdown.frame));
    
}

/**
 *  切换城市
 */
- (IBAction)changeCity {
    [self.popover dismissPopoverAnimated:YES];
    
    MTCityViewController* city = [[MTCityViewController alloc]init];
    MTNavigationController* nav = [[MTNavigationController alloc]initWithRootViewController:city];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
    
    // self.presentedViewController会引用着被modal出来的控制器
    // modal出来的是MTNavigationController
    // dismiss掉的应该也是MTNavigationController
}

#pragma mark - MTHomeDropdownDataSource
-(NSInteger)numberOfRowsInMainTable:(MTHomeDropdown *)homeDropdown{
    return self.regions.count;
}

-(NSString *)homeDropdown:(MTHomeDropdown *)homeDropdown titleForRowInMainTable:(NSInteger)row{
    MTRegion* region = self.regions[row];
    return region.name;
}

-(NSArray *)homeDropdown:(MTHomeDropdown *)homeDropdown subdataForRowInMainTable:(NSInteger)row{
    MTRegion* region = self.regions[row];
    return region.subregions;
}

#pragma mark - MTHomeDropdownDelegate
-(void)homeDropdown:(MTHomeDropdown *)homeDropdown didSelectRowInMainTable:(NSInteger)row{
     MTRegion* region = self.regions[row];
    if (region.subregions.count == 0) {
        //发出通知
        [MTNotificationCenter postNotificationName:MTRegionDidChangeNotification object:nil userInfo:@{MTSelectRegion:region}];
    }
}

-(void)homeDropdown:(MTHomeDropdown *)homeDropdown didSelectRowInSubTable:(NSInteger)subrow inMainTable:(NSInteger)mainrow{
    MTRegion* region = self.regions[mainrow];
    //发出通知
    [MTNotificationCenter postNotificationName:MTRegionDidChangeNotification object:nil userInfo:@{MTSelectRegion:region,MTSelectSubregionName:region.subregions[subrow]}];
}
@end
